var wordFileUrl;

function initPage(urlParam, flag) {

    $.ajax({
        type: "get",
        url: urlParam,
        success: function (data) {
            console.log(data);
            if (flag == true) {
                if (data == null || data.totalPage == 0) {
                    $(".page-widget").hide();
                    $(".maintain-right ul").css("text-align", "center");
                    $(".maintain-right ul").text("当前暂无产品");
                }
                else {
                    $(".page-widget").show();
                    $(".page-widget .currentPage").text(data.currentPage);
                    $(".page-widget .totalPage").text(data.totalPage);
                    $(".maintain-right ul").empty();
                    for (var i = 0; i < data.fileNames.length; i++) {
                        $(".maintain-right ul").append(
                         "<li><a href='/wzdisplay/file/forcast/portAndChannelDown?fileName="+ data.fileNames[i] + "'>" + data.fileNames[i] + "</a></li>"
                        //"<li><a href='http://172.21.160.92/portForeRevise/"+ data.fileNames[i] + "'>" + data.fileNames[i] + "</a></li>"
                        );
                    }
                }

            }
            else {
                if (data == null || data.totalPage == 0) {
                    $(".page-widget").hide();
                    $(".maintain-right ul").css("text-align", "center");
                    $(".maintain-right ul").text("当前暂无产品");
                }
                else {
                    $(".page-widget").show();
                    $(".page-widget .currentPage").text(data.currentPage);
                    $(".page-widget .totalPage").text(data.totalPage);
                    $(".maintain-right ul").empty();
                    for (var i = 0; i < data.fileNames.length; i++) {
                        $(".maintain-right ul").append(
                            "<li><a href='http://172.21.160.92/forcastWord/" + data.firDir + "/" + data.fileNames[i] + "'>" + data.fileNames[i] + "</a></li>"
                        );
                    }
                }
            }
        }
    });
}


function pagePre() {
    if ($(".page-widget .currentPage").text() == "1") {
        return;
    } else {
        var aimPage = parseInt($(".page-widget .currentPage").text()) - 1;
        $(".page-widget .currentPage").text(aimPage);
        initPage(wordFileUrl + aimPage);
    }
}

function pageNext(data) {
    if ($(".page-widget .currentPage").text() == $(".page-widget .totalPage").text()) {
        return;
    } else {
        var aimPage = parseInt($(".page-widget .currentPage").text()) + 1;
        $(".page-widget .currentPage").text(aimPage);
        initPage(wordFileUrl + aimPage);
    }
}

function pageGo() {
    var aimVal = $(".aimPage").val()
    if (aimVal == null || aimVal > $(".page-widget .totalPage").text() || aimVal < 1) {
        return;
    } else {
        initPage(wordFileUrl + aimVal);
    }
}

$(function () {
    $(".maintain-left ul li").each(function (index, el) {
        $(el).bind("click", function () {
            $(".maintain-left ul li").removeClass('selected');
            $(el).addClass('selected');
            switch (index) {
                //portForeRevise
                case 0: {
                    wordFileUrl = "/wzdisplay/file/forcast/important?pageNum=";
                    initPage(wordFileUrl);
                    break;
                }
                case 1: {

                    wordFileUrl = "/wzdisplay/file/forcast/portAndChannel?pageNum=";
                    initPage(wordFileUrl, true);
                    break;
                }
                case 2: {
                    wordFileUrl = "/wzdisplay/file/forcast/threeMonthly?pageNum=";
                    initPage(wordFileUrl);
                    break;
                }
                case 3: {
                    wordFileUrl = "/wzdisplay/file/forcast/weekly?pageNum=";
                    //initPage(wordFileUrl);
                    break;
                }
                case 4: {
                    wordFileUrl = "/wzdisplay/file/forcast/other1?pageNum=";
                    //initPage(wordFileUrl);
                    break;
                }
                default:
                    break;

            }
        });

    });


    $(".page-widget .pre").bind("click", pagePre);
    $(".page-widget .next").bind("click", pageNext);
    $(".page-widget .go").bind("click", pageGo);

    //触发初始选中的li click事件
    $(".maintain-left ul .selected").trigger("click");
})
